package com.ithhc.chain.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.transaction.ChainedTransactionManager;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class TransactionConfig {
    @Autowired
    @Qualifier("orderDataSource")
    private DataSource orderDataSource;

    @Autowired
    @Qualifier("userTransactionManager")
    private DataSourceTransactionManager userTransactionManager;

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        DataSourceTransactionManager orderTransactionManager = new DataSourceTransactionManager(orderDataSource);
        ChainedTransactionManager chainedTransactionManager = new ChainedTransactionManager( userTransactionManager,orderTransactionManager);
        return chainedTransactionManager;
    }
}
